/**  @addtogroup main_module main module documentation
 *  @{
 */
/* MODULE main */

/* Including necessary configuration files. */
#include "sdk_project_config.h"

/* User includes */
#include "osal.h"
#include "pins_driver.h"

/* 蜂鸣器引脚定义 - 修正版本 */
// 方案1：Arduino接口 (推荐)
#define BUZZER_PORT PORTC
#define BUZZER_PIN  4U /* Arduino D5接口，J13-6 */

// 方案2：树莓派接口 (如果您连接到J12-32)
// #define BUZZER_PORT PORTB
// #define BUZZER_PIN  10U   /* RaspPi_32接口，J12-32，实际对应GPIO42 */

/* LED指示灯定义 */
#define LED_SYS_PORT  PORTC
#define LED_SYS_PIN   18U /* SYS_LED (LED2) */
#define LED_IND1_PORT PORTC
#define LED_IND1_PIN  28U /* IND1_LED (LED3) */
#define LED_IND2_PORT PORTD
#define LED_IND2_PIN  14U /* IND2_LED (LED4) */

/* 音符频率定义 (Hz) */
#define NOTE_C4   262 // Do
#define NOTE_D4   294 // Re
#define NOTE_E4   330 // Mi
#define NOTE_F4   349 // Fa
#define NOTE_G4   392 // So
#define NOTE_A4   440 // La
#define NOTE_B4   494 // Si
#define NOTE_C5   523 // Do (高音)
#define NOTE_REST 0   // 休止符

/* 节拍定义 (ms) */
#define BEAT_QUARTER 400 // 四分音符
#define BEAT_EIGHTH  200 // 八分音符
#define BEAT_HALF    800 // 二分音符

/* PIN配置结构体 */
static pin_settings_config_t buzzerPinConfig = {
    .base           = BUZZER_PORT,
    .pinPortIdx     = BUZZER_PIN,
    .pullConfig     = PORT_INTERNAL_PULL_NOT_ENABLED,
    .driveSelect    = PORT_STR3_DRIVE_STRENGTH, /* 20mA驱动能力 */
    .mux            = PORT_MUX_ALT1,            /* 配置为GPIO */
    .isGpio         = true,
    .direction      = GPIO_OUTPUT_DIRECTION,
    .initValue      = 0U,
    .intConfig      = PORT_INT_DISABLED,
    .clearIntFlag   = false,
    .debounceEnable = false,
};

/* LED配置结构体 */
static pin_settings_config_t ledConfigs[] = {
    // LED2 (SYS_LED)
    {
        .base           = LED_SYS_PORT,
        .pinPortIdx     = LED_SYS_PIN,
        .pullConfig     = PORT_INTERNAL_PULL_NOT_ENABLED,
        .driveSelect    = PORT_STR2_DRIVE_STRENGTH,
        .mux            = PORT_MUX_ALT1,
        .isGpio         = true,
        .direction      = GPIO_OUTPUT_DIRECTION,
        .initValue      = 0U,
        .intConfig      = PORT_INT_DISABLED,
        .clearIntFlag   = false,
        .debounceEnable = false,
    },
    // LED3 (IND1_LED)
    {
        .base           = LED_IND1_PORT,
        .pinPortIdx     = LED_IND1_PIN,
        .pullConfig     = PORT_INTERNAL_PULL_NOT_ENABLED,
        .driveSelect    = PORT_STR2_DRIVE_STRENGTH,
        .mux            = PORT_MUX_ALT1,
        .isGpio         = true,
        .direction      = GPIO_OUTPUT_DIRECTION,
        .initValue      = 0U,
        .intConfig      = PORT_INT_DISABLED,
        .clearIntFlag   = false,
        .debounceEnable = false,
    },
    // LED4 (IND2_LED)
    {
        .base           = LED_IND2_PORT,
        .pinPortIdx     = LED_IND2_PIN,
        .pullConfig     = PORT_INTERNAL_PULL_NOT_ENABLED,
        .driveSelect    = PORT_STR2_DRIVE_STRENGTH,
        .mux            = PORT_MUX_ALT1,
        .isGpio         = true,
        .direction      = GPIO_OUTPUT_DIRECTION,
        .initValue      = 0U,
        .intConfig      = PORT_INT_DISABLED,
        .clearIntFlag   = false,
        .debounceEnable = false,
    }};

/* 函数声明 */
static void buzzer_init(void);
static void led_init(void);
static void buzzer_tone(uint16_t frequency, uint16_t duration);
static void buzzer_stop(void);
static void led_all_on(void);
static void led_all_off(void);
static void led_flash_pattern(uint8_t pattern, uint8_t count);
static void play_gentle_alarm(void);

/**
  @brief 初始化LED指示灯
*/
static void led_init(void)
{
    /* 初始化所有LED */
    for (int i = 0; i < 3; i++) {
        PINS_DRV_Init(1, &ledConfigs[i]);
    }

    /* 确保所有LED初始为关闭状态 */
    led_all_off();
}

/**
  @brief 点亮所有LED
*/
static void led_all_on(void)
{
    PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 1);
    PINS_DRV_WritePin(LED_IND1_PORT, LED_IND1_PIN, 1);
    PINS_DRV_WritePin(LED_IND2_PORT, LED_IND2_PIN, 1);
}

/**
  @brief 关闭所有LED
*/
static void led_all_off(void)
{
    PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 0);
    PINS_DRV_WritePin(LED_IND1_PORT, LED_IND1_PIN, 0);
    PINS_DRV_WritePin(LED_IND2_PORT, LED_IND2_PIN, 0);
}

/**
  @brief LED闪烁模式
*/
static void led_flash_pattern(uint8_t pattern, uint8_t count)
{
    for (uint8_t i = 0; i < count; i++) {
        switch (pattern) {
        case 1: // 快闪
            led_all_on();
            OS_DelayMs(100);
            led_all_off();
            OS_DelayMs(100);
            break;

        case 2: // 慢闪
            led_all_on();
            OS_DelayMs(500);
            led_all_off();
            OS_DelayMs(500);
            break;

        case 3: // 流水灯
            PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 1);
            OS_DelayMs(200);
            PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 0);

            PINS_DRV_WritePin(LED_IND1_PORT, LED_IND1_PIN, 1);
            OS_DelayMs(200);
            PINS_DRV_WritePin(LED_IND1_PORT, LED_IND1_PIN, 0);

            PINS_DRV_WritePin(LED_IND2_PORT, LED_IND2_PIN, 1);
            OS_DelayMs(200);
            PINS_DRV_WritePin(LED_IND2_PORT, LED_IND2_PIN, 0);
            OS_DelayMs(200);
            break;

        default:
            break;
        }
    }
}

/**
  @brief 初始化蜂鸣器
*/
static void buzzer_init(void)
{
    /* 初始化蜂鸣器引脚 */
    PINS_DRV_Init(1, &buzzerPinConfig);

    /* 确保蜂鸣器初始为关闭状态 */
    PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);

    /* LED指示初始化完成 */
    led_flash_pattern(1, 5); // 快闪5次
    OS_DelayMs(500);
}

/**
  @brief 蜂鸣器发声 - 改进的PWM实现
*/
static void buzzer_tone(uint16_t frequency, uint16_t duration)
{
    if (frequency == 0) {
        buzzer_stop();
        OS_DelayMs(duration);
        return;
    }

    /* LED指示正在播放 */
    PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 1);

    /* 频率限制 */
    if (frequency < 100)
        frequency = 100;
    if (frequency > 5000)
        frequency = 5000;

    /* 计算周期 - 使用毫秒级延时 */
    uint32_t period_ms      = 1000 / frequency;
    uint32_t half_period_ms = period_ms / 2;

    if (half_period_ms < 1)
        half_period_ms = 1;

    uint32_t total_cycles = duration / period_ms;
    if (total_cycles < 5)
        total_cycles = 5;

    /* 生成方波 */
    for (uint32_t i = 0; i < total_cycles; i++) {
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 1);
        OS_DelayMs(half_period_ms);
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
        OS_DelayMs(half_period_ms);
    }

    /* 关闭输出 */
    buzzer_stop();
    PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 0);
}

/**
  @brief 停止蜂鸣器
*/
static void buzzer_stop(void)
{
    PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
}

/**
  @brief 播放舒缓温和的报警音
*/
static void play_gentle_alarm(void)
{
    /* 舒缓的报警音模式 - 低频到高频渐变 */

    /* 第一组：低频温和提示音 (500Hz) */
    for (int i = 0; i < 3; i++) {
        PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 1);
        buzzer_tone(500, 800); // 500Hz, 0.8秒
        PINS_DRV_WritePin(LED_SYS_PORT, LED_SYS_PIN, 0);
        OS_DelayMs(600); // 0.6秒间隔
    }

    OS_DelayMs(1000); // 1秒停顿

    /* 第二组：中频舒缓音 (750Hz) */
    for (int i = 0; i < 3; i++) {
        PINS_DRV_WritePin(LED_IND1_PORT, LED_IND1_PIN, 1);
        buzzer_tone(750, 800); // 750Hz, 0.8秒
        PINS_DRV_WritePin(LED_IND1_PORT, LED_IND1_PIN, 0);
        OS_DelayMs(600); // 0.6秒间隔
    }

    OS_DelayMs(1000); // 1秒停顿

    /* 第三组：高频轻柔音 (1000Hz) */
    for (int i = 0; i < 2; i++) {
        PINS_DRV_WritePin(LED_IND2_PORT, LED_IND2_PIN, 1);
        buzzer_tone(1000, 600); // 1000Hz, 0.6秒
        PINS_DRV_WritePin(LED_IND2_PORT, LED_IND2_PIN, 0);
        OS_DelayMs(800); // 0.8秒间隔
    }

    /* 关闭所有LED */
    led_all_off();
    OS_DelayMs(2000); // 2秒结束停顿
}

/**
  @brief The main function for the project.
*/
int main(void)
{
    /* 初始化LED指示灯 */
    led_init();

    /* 初始化蜂鸣器 */
    buzzer_init();

    /* 短暂延迟确保系统稳定 */
    OS_DelayMs(500);

    /* 直接开始播放舒缓报警音 */
    for (;;) {
        /* 播放舒缓温和的报警音 */
        play_gentle_alarm();

        /* 播放间隔 */
        OS_DelayMs(3000);
    }
}

/* END main */
/*!
** @}
*/